﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TreatmentsViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The Treatments View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// The Treatments View Model.
    /// </summary>
    public class TreatmentsViewModel : ITreatmentsViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// The region manager instance.
        /// </summary>
        private readonly IRegionManager regionManagerInstance;

        /// <summary>
        /// The treatment add view instance.
        /// </summary>
        private readonly ITreatmentAddView treatmentAddViewInstance;

        /// <summary>
        /// The treatment edit view instance.
        /// </summary>
        private readonly ITreatmentEditView treatmentEditViewInstance;

        /// <summary>
        /// Gets the studies.
        /// </summary>
        /// 
        private ObservableCollection<Treatment> treatments;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TreatmentsViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region Manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        /// <param name="treatmentAddView">
        /// The treatment Add View.
        /// </param>
        /// <param name="treatmentEditView">
        /// The treatment Edit View.
        /// </param>
        public TreatmentsViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService, ITreatmentAddView treatmentAddView, ITreatmentEditView treatmentEditView)
        {
            this.StudiesServiceInstance = studiesService;
            this.regionManagerInstance = regionManager;

            // Child Views: Add
            this.treatmentAddViewInstance = treatmentAddView;
            this.regionManagerInstance.Regions["MainRegion"].Add(treatmentAddView);

            this.treatmentEditViewInstance = treatmentEditView;
            this.regionManagerInstance.Regions["MainRegion"].Add(treatmentEditView);


            // Wire up the exit event for child views)
            this.treatmentAddViewInstance.ViewExit += this.ChildViewExit;
            this.treatmentEditViewInstance.ViewExit += this.ChildViewExit;

            // Setting up command delegates 
            this.EditTreatment = new DelegateCommand<Treatment>(this.OnEditTreatment);
            this.AddTreatment = new DelegateCommand(this.OnAddTreatment);
            this.RemoveTreatment = new DelegateCommand<Treatment>(this.OnRemoveTreatment);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the add treatment command.
        /// </summary>
        public ICommand AddTreatment { get; private set; }

        /// <summary>
        /// Gets the edit treatment command.
        /// </summary>
        public ICommand EditTreatment { get; private set; }

        /// <summary>
        /// Gets the remove treatment command.
        /// </summary>
        public ICommand RemoveTreatment { get; private set; }

        /// <summary>
        /// Gets Treatments.
        /// </summary>
        public ObservableCollection<Treatment> Treatments
        {
            get
            {
                this.treatments=this.StudiesServiceInstance.GetAllTreatments();
                return treatments;
            }
 
            private set
            {
                this.treatments = value;
            }
        }

        /// <summary>
        /// Gets or sets View.
        /// </summary>
        public ITreatmentsView View { get; set; }

        /// <summary>
        /// Gets A studies service instance
        /// </summary>
        protected IStudiesUIProxyService StudiesServiceInstance { get; private set; }

        #endregion

        #region Implemented Interfaces

        #region ITreatmentsViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // load all the studies into the studies collection
            this.Treatments = this.StudiesServiceInstance.GetAllTreatments();
        }

        /// <summary>
        /// Called when [add treatment].
        /// </summary>
        public void OnAddTreatment()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.treatmentAddViewInstance);
        }

        /// <summary>
        /// Called when [edit treatment].
        /// </summary>
        /// <param name="treatment">
        /// The treatment.
        /// </param>
        public void OnEditTreatment(Treatment treatment)
        {
            if (treatment == null)
            {
                return;
            }

            this.regionManagerInstance.Regions["MainRegion"].Activate(this.treatmentEditViewInstance);
        }

        /// <summary>
        /// Called when [remove treatment].
        /// </summary>
        /// <param name="treatment">
        /// The treatment.
        /// </param>
        public void OnRemoveTreatment(Treatment treatment)
        {
            if (treatment == null)
            {
                return;
            }

            this.StudiesServiceInstance.RemoveTreatment(treatment);
            this.RaiseOnPropertyChanged("Treatments");
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// The child view exit.
        /// </summary>
        private void ChildViewExit()
        {
            this.regionManagerInstance.Regions["MainRegion"].Activate(this.View);
            this.RaiseOnPropertyChanged("Treatments");
        }

        #endregion
    }
}
